package cn.maiyar.common.config;

import cn.maiyar.common.constant.CommonConstant;
import com.nepxion.banner.BannerConstant;
import com.nepxion.banner.Description;
import com.nepxion.banner.DescriptionBanner;
import com.nepxion.banner.LogoBanner;
import com.taobao.text.Color;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 初始化 Banner
 *
 * @author c.c
 */
@Slf4j
@Configuration
public class BannerInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        log.debug("初始化 Banner!");
        if (!(applicationContext instanceof AnnotationConfigApplicationContext)) {
            LogoBanner logoBanner = new LogoBanner(BannerInitializer.class, "logo.txt", "Welcome to Maiyar.", 5, 6, new Color[5], true);
            this.show(logoBanner,
                    new Description(BannerConstant.VERSION + ":", CommonConstant.PROJECT_VERSION, 0, 1)
                    , new Description("maiyar:", "https://www.maiyar.com", 0, 1)
                    , new Description("Admin:", "https://admin.maiyar.com", 0, 1)
                    , new Description("GitLab:", "https://gitlab.maiyar.com", 0, 1)
                    , new Description("Nacos:", "https://nacos.maiyar.com/nacos", 0, 1)
                    , new Description("Jenkins:", "https://jenkins.maiyar.com", 0, 1)
                    , new Description("Nexus3:", "https://nexus3.maiyar.com/", 0, 1)
                    , new Description("SonarQube:", "https://sonarqube.maiyar.com", 0, 1)
                    , new Description("ZipKin:", "https://zipkin.maiyar.com", 0, 1)
                    , new Description("Sentinel:", "https://sentinel.maiyar.com", 0, 1)
                    , new Description("Harbor:", "https://harbor.maiyar.com", 0, 1)
                    , new Description("Grafana:", "https://grafana.maiyar.com", 0, 1)
                    , new Description("Prometheus:", "https://prometheus.maiyar.com", 0, 1)
                    , new Description("Skywalking:", "https://skywalking.maiyar.com", 0, 1)
            );
        }
    }

    private void show(LogoBanner logoBanner, Description... descriptionList) {
        String bannerShown = System.getProperty(BannerConstant.BANNER_SHOWN, "true");
        if (!Boolean.parseBoolean(bannerShown)) {
            return;
        }
        String bannerShownAnsiMode = System.getProperty(BannerConstant.BANNER_SHOWN_ANSI_MODE, "false");
        if (Boolean.parseBoolean(bannerShownAnsiMode)) {
            log.info(logoBanner.getBanner());
        } else {
            log.info(logoBanner.getPlainBanner());
        }
        List<Description> descriptions = new ArrayList<>(Arrays.asList(descriptionList));
        DescriptionBanner descriptionBanner = new DescriptionBanner();
        log.info(descriptionBanner.getBanner(descriptions));
    }
}
